import 'package:flutter/material.dart';
import 'package:net/http/dao/profile_dao.dart';
import 'package:net/model/profile_mo.dart';
import 'package:net/page/dark_mode_item.dart';
import 'package:hi_base/log.dart';
import 'package:hi_base/view_util.dart';
import 'package:net/widget/bannefit_card.dart';
import 'package:net/widget/course_card.dart';
import 'package:net/widget/h_banner.dart';
import 'package:net/widget/hi_blur.dart';
import 'package:net/widget/hi_flexible_header.dart';
import 'package:net/widget/navigation_bar.dart';
import 'package:net/utils/view_util.dart';

///我的
class ProfilePage extends StatefulWidget {
  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  ProfileMo _profileMo;
  ScrollController _controller = ScrollController();
  @override
  void initState() {
    super.initState();
    var statusStyle = StatusStyle.DARK_CONTENT;
    changeStatusBar(color: Colors.transparent, statusStyle: statusStyle);
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: NestedScrollView(
      controller: _controller,
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return <Widget>[_buildAppbar()];
      },
      body: ListView(
        padding: EdgeInsets.only(top: 10),
        children: [..._buildContentList()],
      ),
    ));
  }

  void _loadData() async {
    try {
      ProfileMo result = await ProfileDao.get();
      // print(result.toJson());
      setState(() {
        _profileMo = result;
      });
    } catch (e) {
      hyLog(e, StackTrace.current);
    }
  }

  _buildAppbar() {
    return SliverAppBar(
      //扩展高度
      expandedHeight: 150,
      // 标题栏是否固定
      pinned: true,
      // 定义固定空间
      flexibleSpace: FlexibleSpaceBar(
        title: _buildHead(),
        collapseMode: CollapseMode.parallax,
        titlePadding: EdgeInsets.only(left: 0),
        background: Stack(
          children: [
            Positioned.fill(
                child: cachedImage(
                    'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3516595062,3107613257&fm=26&gp=0.jpg')),
            Positioned(child: HBlur(sigma: 20)),
            Positioned(
              child: _buildProfileTab(),
              bottom: 0,
              left: 0,
              right: 0,
            )
          ],
        ),
      ),
    );
  }

  _buildHead() {
    if (_profileMo == null) return Container();
    return HFlexibleHeader(
      controller: _controller,
      face: _profileMo.face,
      name: _profileMo.name,
    );
  }

  _buildContentList() {
    if (_profileMo == null) return [];
    return [
      _buildBanner(),
      CourseCard(courseList: _profileMo.courseList),
      BannerFitCard(bennefitList: _profileMo.benefitList),
      DarkModelItem()
    ];
  }

  _buildBanner() {
    return HBanner(
      bannerList: _profileMo.bannerList,
      bannerHeight: 120,
      padding: EdgeInsets.only(left: 10, right: 10),
    );
  }

  _buildProfileTab() {
    if (_profileMo == null) return Container();
    return Container(
      padding: EdgeInsets.only(top: 5, bottom: 5),
      decoration: BoxDecoration(color: Colors.white54),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _buildIconText('收藏', _profileMo.favorite),
          _buildIconText('点赞', _profileMo.like),
          _buildIconText('浏览', _profileMo.browsing),
          _buildIconText('金币', _profileMo.coin),
          _buildIconText('粉丝', _profileMo.fans),
        ],
      ),
    );
  }

  _buildIconText(String text, int count) {
    return Column(
      children: [
        Text(
          '$count',
          style: TextStyle(fontSize: 15, color: Colors.black87),
        ),
        Text(
          '$text',
          style: TextStyle(
            fontSize: 12,
            color: Colors.grey[600],
          ),
        ),
      ],
    );
  }
}
